package com.website.company.controller;

import com.website.company.entity.CompanyDevelopment;
import com.website.company.entity.Product;
import com.website.company.mapper.CompanyDevelopmentMapper;
import com.website.company.service.impl.CompanyDevelopmentServiceImpl;
import com.website.company.utils.ProductVO;
import com.website.company.utils.RelationDTO;
import com.website.company.utils.ResponseBo;
import com.website.company.utils.ResponseList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/sub")
public class SubController {

    @Autowired
    private CompanyDevelopmentMapper companyDevelopmentMapper;
    @Autowired
    private CompanyDevelopmentServiceImpl service;

    @GetMapping("/getList")
    public List<CompanyDevelopment> getList() {
        return companyDevelopmentMapper.selectList(null);
    }

    @GetMapping("/getSub")
    ResponseBo getProduct(@RequestParam("id") Integer id) {
        try {
            CompanyDevelopment companyDevelopment = companyDevelopmentMapper.getById(id);
            return ResponseBo.ok(companyDevelopment);
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    @PostMapping("/delete")
    ResponseBo delete(@RequestBody Integer[] ids) {
        try {
            service.delete(ids);
            return ResponseBo.ok();
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    @PostMapping("/saveSub")
    ResponseBo saveProduct(@RequestBody CompanyDevelopment companyDevelopment) {
        if (companyDevelopment.getId() == null) {
            try {
                service.add(companyDevelopment);
                return ResponseBo.ok();
            } catch (Exception e) {
                return ResponseBo.error(e.getMessage());
            }
        } else {
            try {
                service.update(companyDevelopment);
                return ResponseBo.ok(companyDevelopment);
            } catch (Exception e) {
                return ResponseBo.error(e.getMessage());
            }
        }
    }

}
